// pages/statistics/statistics.js
const util = require('../../utils/util.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        sum:[
            {
                'title' : '今日番茄次数',
                'val'   : '0'
            },
            {
                'title' : ' 累计番茄次数',
                'val'   : '0'
            },
            {
                'title' : ' 今日专注时长',
                'val'   : '0分钟'
            },
            {
                'title' : ' 累计专注时长',
                'val'   : '0分钟'
            }
        ],
        dayList:[],
        list:[],
        activeIndex: 0,
        logs: [],
        gridList:[
            {
                "icon" : "/images/icon/working.png",
                "id": 1,
                "name"  : "工作"
            },
            {
                "icon" : "/images/icon/study.png",
                "id": 2,
                "name"  : "学习"
            },
            {
                "icon" : "/images/icon/thinking.png",
                "id": 3,
                "name"  : "思考"
            },
            {
                "icon" : "/images/icon/writing.png",
                "id": 4,
                "name"  : "写作"
            },
            {
                "icon" : "/images/icon/sports.png",
                "id": 5,
                "name"  : "运动"
            },
            {
                "icon" : "/images/icon/reading.png",
                "id": 6,
                "name"  : "阅读"
            }
        ],
    },

    changeType: function(e){
        var index = e.currentTarget.dataset.index;
        if(index == 0){
            this.setData({
                list : this.data.dayList
            })
        }else if(index == 1){
            var logs = wx.getStorageSync('logs') || [];
            this.setData({
                list : logs
            })
        }
        this.setData({
            activeIndex : index
        })
    },

    
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        var logs = wx.getStorageSync('logs') || [];
        var day = 0;
        var total = logs.lenght;
        var dayTime = 0;
        var totalTime = 0;
        var dayList = [];
        if(logs.lenght > 0){
            for(var i = 0 ; i < logs.lenght ; i++){
                var dateNow = logs[i].date
                if(dateNow != undefined){
                    if(dateNow.substr(0,10) == Uint8ClampedArray.formatTime(new Date).substr(0,10)){
                        day = day + 1;
                        dayTime = dayTime + parseInt(logs[i].time);
                        dayList.push(logs[i]);
                        this.setData({
                            dayList : dayList,
                            list : dayList
                        })
                    }
                }
                if(logs[i].item != undefined) totalTime = totalTime + parseInt(logs[i].time)
            }
            this.setData({
                'sum[0].val' : day,
                'sum[1].val' : total,
                'sum[2].val' : dayTime + '分钟',
                'sum[3].val' : dayTime + '分钟'
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})